/*
 * ProblemChooserDialog.java
 *
 * Created on 2. Juli 2007, 13:48
 */

package org.australia.ui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.DefaultListModel;
import javax.swing.JTextField;
import javax.swing.ListModel;

/**
 *
 * @author  Daniel_h4x
 */
public class ProblemChooserDialog extends javax.swing.JDialog {
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    
    private JTextField retValField;
    
    /** Creates new form ProblemChooserDialog
     * @param parent the parent frame to which this Dialog is displayed (correct positioning)
     * @param modal boolean to indicate whether this Dialog should be modal
     * @param field The TextField to where the chosen problem instance's name will be written to
     */
    public ProblemChooserDialog(java.awt.Frame parent, boolean modal, JTextField field) {
        super(parent, modal);
        initComponents();
        pack();
        Rectangle parentBounds = parent.getBounds();
        Dimension size = getSize();
        // Center in the parent
        int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
        int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
        setLocation(new Point(x, y));
        this.retValField = field;
    }
    
    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        HolmbergList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        BocciaList = new javax.swing.JList();
        InstructionLabel = new javax.swing.JLabel();
        HolmbergListLabel = new javax.swing.JLabel();
        BocciaListLabel = new javax.swing.JLabel();

        setTitle("Auswahl der Probleminstanz");
        setModal(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        HolmbergList.setModel(getHolmbergModel());
        HolmbergList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        HolmbergList.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP);
        HolmbergList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                HolmbergListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(HolmbergList);

        BocciaList.setModel(getBocciaModel());
        BocciaList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        BocciaList.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP);
        BocciaList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                BocciaListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(BocciaList);

        InstructionLabel.setText("Bitte w\u00e4hlen Sie eine Probleminstanz aus!");

        HolmbergListLabel.setText("Holmberg-Instanzen");

        BocciaListLabel.setText("Boccia-Instanzen");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .add(okButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(cancelButton))
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(HolmbergListLabel)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 195, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(BocciaListLabel)
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 191, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                    .add(InstructionLabel))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(new java.awt.Component[] {cancelButton, okButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(InstructionLabel)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(HolmbergListLabel)
                    .add(BocciaListLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2))
                .add(8, 8, 8)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
private void HolmbergListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_HolmbergListValueChanged
    
    if(BocciaList.isEnabled()){
        BocciaList.setEnabled(false);
    }
    //    else{
    //        BocciaList.setEnabled(true);
    
    //    }
}//GEN-LAST:event_HolmbergListValueChanged

    private void BocciaListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_BocciaListValueChanged
        
        if(HolmbergList.isEnabled()){
            HolmbergList.setEnabled(false);
        }
        //        else{
        //            HolmbergList.setEnabled(true);
        //        }
}//GEN-LAST:event_BocciaListValueChanged
    
    private DefaultListModel getBocciaModel(){
        
        DefaultListModel model = new DefaultListModel();
        
        
        for(int i = 0; i < 75; i++){
            
            for (int j = 1; j <= 15; j++) {
                
                if(i >= 0 && i <= 15){
                    model.add(i, "i5050_"+j+".plc");
                } else if(i >= 15 && i <= 30){
                    model.add(i, "i5075_"+j+".plc");
                } else if(i >= 30 && i <= 45){
                    model.add(i, "i50100_"+j+".plc");
                } else if(i >= 45 && i <= 60){
                    model.add(i, "i75100_"+j+".plc");
                } else if(i >= 60 && i <= 75){
                    model.add(i, "i7575_"+j+".plc");
                }
                
            }
            
        }
        return model;
    }
    
    private DefaultListModel getHolmbergModel(){
        
        DefaultListModel model = new DefaultListModel();
        
        for (int i = 0; i < 71; i++) {
            String name = "P"+(i+1);
            model.add(i, name);
        }
        return model;
    }
    
    
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        this.retValField.setText("The Chosen Problem");
        doClose(RET_OK);
    }//GEN-LAST:event_okButtonActionPerformed
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog
    
    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ProblemChooserDialog dialog = new ProblemChooserDialog(new javax.swing.JFrame(), true, new JTextField());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList BocciaList;
    private javax.swing.JLabel BocciaListLabel;
    private javax.swing.JList HolmbergList;
    private javax.swing.JLabel HolmbergListLabel;
    private javax.swing.JLabel InstructionLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
    
    private int returnStatus = RET_CANCEL;
}
